//
//  BN_ChatHelper.m
//  BabyNow
//
//  Created by 闫建刚 on 14-5-12.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_ChatHelper.h"
#import "IMB_NetWorkJsonRespOperation.h"
#import "BN_UserInfo.h"
#import "BN_UserHelper.h"

@interface BN_ChatHelper()
{
    // 聊天详情页码
    int pageNo_;
    
    // 聊天详情字典
    NSMutableDictionary *chatDetailDict_;
    
    // 聊天详情数组
    NSMutableArray *chatDetailArr_;
    
    
}

/**
 *  查询聊天详情队列
 */
@property (nonatomic,strong) NSOperationQueue *queryChatDetailQueue;

/**
 *  查询联系人列表队列
 */
@property (nonatomic,strong) NSOperationQueue *queryChatPersonListQueue;

/**
 *  查询聊天会话表队列
 */
@property (nonatomic,strong) NSOperationQueue *queryChatListQueue;

/**
 *  查询指定页码的聊天详情
 *
 *  @param userId 对象userId
 *  @param pageno 页码
 */
- (void)queryChatDetailWithUserId:(NSString*)userId
                           pageno:(int)pageno;

@end

@implementation BN_ChatHelper

#pragma mark - Extend method

// 查询与指定对象的第几页聊天详情
- (void)queryChatDetailWithUserId:(NSString *)userId pageno:(int)pageno{
    
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_CHAT_DETAIL_LIST requestMethod:NET_REQUEST_GET paramsValues:@{kAccount:userInfo.account,kPassword:userInfo.password,kUserId:userId,kPageNo:[NSString stringWithFormat:@"%d",pageno]} completeHandler:^(id result, NSString *errorMessage) {
        
        if (result) {
            if ([result[kErrorCode]intValue] == 0) {
                if(result[kUserInfo]){
                    chatDetailDict_[kUserInfo] = result[kUserInfo];
                }
                if (result[kChatInfo] && [result[kChatInfo] isKindOfClass:[NSArray class]] && [result[kChatInfo] count] > 0) {
                    pageNo_ = pageno;
                    if (pageNo_ == 1) {
                        [chatDetailArr_ removeAllObjects];
                    }
                    [chatDetailArr_ addObjectsFromArray:result[kChatInfo]];
                    chatDetailDict_[kChatInfo] = chatDetailArr_;
                    self.queryChatDetailCompleteHandler(chatDetailDict_,errorMessage);
                }else{
                    if (pageno == 1) {
                        self.queryChatDetailCompleteHandler(nil,NET_ERROR_MSG_FOR_NO_DATA);
                    }else{
                        self.queryChatDetailCompleteHandler(nil,NET_ERROR_MSG_FOR_NO_MORE_DATA);
                    }
                }
            }else{
                self.queryChatDetailCompleteHandler(nil,result[kErrorMessage]);
            }
        }else{
            self.queryChatDetailCompleteHandler(nil,errorMessage);
        }
    }];
    [self.queryChatDetailQueue addOperation:operation];
}

#pragma mark - Interface method

// 刷新与指定对象的聊天记录
- (void)refreshChatDetailListWithUserId:(NSString *)userId{
    if (self.queryChatDetailQueue.operationCount > 0) {
        self.queryChatDetailCompleteHandler(nil,nil);
        return;
    }
    [self queryChatDetailWithUserId:userId pageno:1];
}

// 查询下一页聊天详情记录
- (void)queryNextPageChatDetailList:(NSString*)userId{
    if (self.queryChatDetailQueue.operationCount > 0) {
        self.queryChatDetailCompleteHandler(nil,nil);
        return;
    }
    [self queryChatDetailWithUserId:userId pageno:pageNo_ + 1];
}

// 查询联系人列表
- (void)queryChatPersonList:(DataHandleComplete)completeHandler{
    if (self.queryChatPersonListQueue.operationCount > 0) {
        completeHandler(nil,nil);
        return;
    }
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_CHAT_PERSON_LIST requestMethod:NET_REQUEST_GET paramsValues:@{kAccount:userInfo.account,kPassword:userInfo.password} completeHandler:^(id result, NSString *errorMessage) {
        if (result) {
            if ([result[kErrorCode]intValue] == 0) {
                
                NSMutableDictionary *personDict;
                if (result[kTechInfo]) {
                    if (!personDict) {
                        personDict = [NSMutableDictionary dictionary];
                    }
                    personDict[kTechInfo] = result[kTechInfo];
                }
                if (result[kPareInfo]) {
                    if (!personDict) {
                        personDict = [NSMutableDictionary dictionary];
                    }
                    personDict[kPareInfo] = result[kPareInfo];
                }
                if (personDict) {
                    completeHandler(personDict,errorMessage);
                }else{
                    completeHandler(nil,NET_ERROR_MSG_FOR_NO_DATA);
                }
            }else{
                completeHandler(nil,result[kErrorMessage]);
            }
        }else{
            completeHandler(nil,NET_ERROR_MSG_FOR_NO_DATA);
        }
        
    }];
    [self.queryChatPersonListQueue addOperation:operation];
}

// 查询聊天会话记录
- (void)queryChatList:(DataHandleComplete)completeHandler{
    if (self.queryChatListQueue.operationCount > 0) {
        completeHandler(nil,nil);
        return;
    }
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_CHAT_CONTANT_LIST requestMethod:NET_REQUEST_GET paramsValues:@{kAccount:userInfo.account,kPassword:userInfo.password} completeHandler:^(id result, NSString *errorMessage) {
        if (result) {
            if ([result[kErrorCode]intValue] == 0) {
                completeHandler(result[kSessInfo],errorMessage);
            }else{
                completeHandler(nil,result[kErrorMessage]);
            }
        }else{
            completeHandler(nil,NET_ERROR_MSG_FOR_NO_DATA);
        }
        
    }];
    [self.queryChatPersonListQueue addOperation:operation];
}

// 向指定对象发送聊天内容
- (void)sendChatContentWithUserId:(NSString *)userId
                      chatContent:(NSString *)chatContent
                  completeHandler:(DataHandleComplete)handler{
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    IMB_NetWorkJsonRespOperation *operation = [[IMB_NetWorkJsonRespOperation alloc]initWithUrlStr:NET_IF_SEND_CHAT_CONTENT requestMethod:NET_REQUEST_POST paramsValues:@{kAccount:userInfo.account,kPassword:userInfo.password,kUserId:userId,kChatCont:chatContent} completeHandler:^(id result, NSString *errorMessage) {
        if ([result[kErrorCode]intValue] == 0) {
            handler(nil,result[kErrorMessage]);
        }else{
            handler(nil,errorMessage);
        }
    }];
    [[[NSOperationQueue alloc]init]addOperation:operation];
}

// 取消查询聊天会话列表
- (void)cacelQueryChatList{
    [self.queryChatListQueue.operations makeObjectsPerformSelector:@selector(cancelNetAccess)];
}

// 取消查询聊天详情列表
- (void)cancelQueryChatDetailList{
    [self.queryChatDetailQueue.operations makeObjectsPerformSelector:@selector(cancelNetAccess)];
}

// 取消查询联系人列表
- (void)cancelQueryChatPersonList{
    [self.queryChatPersonListQueue.operations makeObjectsPerformSelector:@selector(cancelNetAccess)];
}

#pragma mark - Property method

- (NSOperationQueue*)queryChatListQueue{
    if (!_queryChatListQueue) {
        _queryChatListQueue = [[NSOperationQueue alloc]init];
    }
    return _queryChatListQueue;
}

- (NSOperationQueue*)queryChatPersonListQueue{
    if (!_queryChatPersonListQueue) {
        _queryChatPersonListQueue = [[NSOperationQueue alloc]init];
    }
    return _queryChatPersonListQueue;
}

- (NSOperationQueue*)queryChatDetailQueue{
    if (!_queryChatDetailQueue) {
        _queryChatDetailQueue = [[NSOperationQueue alloc]init];
    }
    return _queryChatDetailQueue;
}

#pragma mark - Life cycle method

- (id)init{
    if (self = [super init]) {
        chatDetailArr_ = [[NSMutableArray alloc]init];
        chatDetailDict_ = [NSMutableDictionary dictionary];
    }
    return self;
}

@end
